#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include "uart.h"
#include "os_api.h"
#include "time_api.h"
#include "debug.h"
#include "ps_api.h"
#include "dev_api.h"

int gettimeofday(struct timeval *tv, void* dummy)
{	 
    unsigned int t;
    t = time(NULL);

    if(tv != NULL){
      tv->tv_sec = t;
      tv->tv_usec = 0;
      return 0;
    }else{
      return -1;
    }
}
#define _STACK_SIZE			0x2000
static void* _task_stack = NULL;

static OSTaskRef _task_ref = NULL;


OSATimerRef _task_delete_timer_ref = NULL;

static void _task(void *ptr);
static void wait_network_ready(void)
{
	int count = 30;
    while(count-- && ps_GetNetworkReady() == FALSE){	
        OSATaskSleep(200);
    }

    if (count <= 0){
        LOG_PRINTF("dev_CtrlReset.\n");
        dev_CtrlReset(0);
    }
}

extern int onenet_connet(void);

static void _task(void *ptr)
{
	wait_network_ready();
	
	onenet_connet();		
}

int main(int argc, char const *argv[])
{
	 int ret;
	
	_task_stack = malloc(_STACK_SIZE);
	
    ret = OSATaskCreate(&_task_ref, _task_stack, _STACK_SIZE, 100, "_task", _task, NULL);
    ASSERT(ret == OS_SUCCESS);

	return ret;
}

